package l1j.server.server.model.damage.magic;

import static l1j.server.server.model.skill.L1SkillId.COUNTER_MIRROR;
import static l1j.server.server.model.skill.L1SkillId.MIND_BREAK;
import l1j.server.server.ActionCodes;
import l1j.server.server.WarTimeController;
import l1j.server.server.model.L1CastleLocation;
import l1j.server.server.model.L1Character;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.model.damage.L1Dmg;
import l1j.server.server.model.damage.L1MagicOnPc;
import l1j.server.server.model.damage.L1NpcMagic;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.templates.L1Skills;
import l1j.server.server.utils.Random;

public class L1MagicDamage_Npc_Pc extends L1Dmg {

	private L1NpcInstance _attacker;

	private L1PcInstance _target;

	private int _attr;

	public L1MagicDamage_Npc_Pc(L1NpcInstance attacker, L1PcInstance target,
			int attr) {
		_attr = attr;
		_attacker = attacker;
		_target = target;
	}

	public void commitMagic(L1Character target) {
		if (Random.chance100(mr_reduction_chance[target.getMr() + 100])) {
			mul((100 - mr_reduction_percent[target.getMr() + 100]) / 100.0);
		}
		if (_attr == 1) {// 地屬性
			mul((100 - attr_reduction_percent[target.getEarth() + 300]) / 100.0);
		} else if (_attr == 2) {// 火屬性
			mul((100 - attr_reduction_percent[target.getFire() + 300]) / 100.0);
		} else if (_attr == 4) {// 水屬性
			mul((100 - attr_reduction_percent[target.getWater() + 300]) / 100.0);
		} else if (_attr == 8) {// 風屬性
			mul((100 - attr_reduction_percent[target.getWind() + 300]) / 100.0);
		}
	}
	
	public void commitAttrByWp(double dmg) {
		if (_target.getAttrStartTimes() > 5) {
			if (_target.getWeapon() == null) {
				return;
			}
			if (_target.getWeapon().getAttrEnchantKind() == 1) {
				int targetattrLevel = _target.getAttrStartTimes() / 5;
				_target.sendPackets(new S_SystemMessage("地屬測試減傷前: " + dmg));
				if (getValue() < 80) {
					add(targetattrLevel);
				} else {
					add(dmg * (0.01 + (0.01 * targetattrLevel)));
				}
				_target.sendPackets(new S_SystemMessage("地屬測試減傷後: " + (dmg - getValue())));
			}
		}
	}
	
	@Override
	public void hurtTarget(boolean actionHurt) {
		commitAttacker(_attacker);
		commitTarget(_target);
		commitMagic(_target);
		_target.receiveDamage(_attacker, getValue());
		if (getValue() > 0 && actionHurt) {
			_target.sendPackets(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
			_target.broadcastPacket(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
		}
	}

	@Override
	public void calcDmg() {
		if (_target.isInvincible()) {
			return;
		}
		L1Skills skill = _attacker.getCurrentSkill();
		if (skill == null) {
			zero();
			return;
		}
		if ((_attacker instanceof L1PetInstance)
				|| (_attacker instanceof L1SummonInstance)) {
			// 目標在安區、攻擊者在安區、NOPVP
			if ((_target.getZoneType() == 1) || (_attacker.getZoneType() == 1)
					|| (_target.checkNonPvP(_target, _attacker))) {
				zero();
				return;
			}
		}

		_attr = skill.getAttr();

		L1NpcMagic nm = new L1NpcMagic(_attacker, this);
		nm.calcNpcDmg();

		int skillId = _attacker.getCurrentSkill().getId();
		// 心靈破壞
		if (skillId == MIND_BREAK && _target.getCurrentMp() >= 5) {
			_target.setCurrentMp(_target.getCurrentMp() - 5);
			add(_attacker.getWis() * 5);
		}
		
		boolean isNowWar = false;
		int castleId = L1CastleLocation.getCastleIdByArea(_target);
		if (castleId > 0) {
			isNowWar = WarTimeController.getInstance().isNowWar(castleId);
		}
		if (!isNowWar) {
			if (_attacker instanceof L1PetInstance) {
				div(8);
			}
			if (_attacker instanceof L1SummonInstance) {
				L1SummonInstance summon = (L1SummonInstance) _attacker;
				if (summon.isExsistMaster()) {
					div(8);
				}
			}
		}

		L1MagicOnPc mop = new L1MagicOnPc(_target, this);
		mop.calcOnPcDmg();

		if (_target.hasSkillEffect(COUNTER_MIRROR)) {
			int npcId = _attacker.getNpcTemplate().get_npcId();
			if ((npcId == 45681) || (npcId == 45682) || (npcId == 45683)
					|| (npcId == 45684)) {
			} else if (!_attacker.getNpcTemplate().get_IsErase()) {
			} else {
				if (_target.getWis() >= Random.nextInt(100)) {
					L1Dmg.get(true, _attr, _target, _attacker).add(getValue())
							.hurtTarget(true);
					_target.sendPackets(new S_SkillSound(_target.getId(), 4395));
					_target.broadcastPacket(new S_SkillSound(_target.getId(),
							4395));
					zero();
					_target.killSkillEffectTimer(COUNTER_MIRROR);
				}
			}
		}
	}

}
